import 'package:flutter/material.dart';

typedef SheetClickCallBack = void Function(int selectIndex, String selectText);

class GKBSheetDialog {
  //index 从上往下 1，2，3，取消是0

  static const double _cellHeight = 50.0;
  static const double _spaceHeight = 5.0;
  static const double _textFontSize = 14.0;
  static const Color _redTextColor = Color(0xFFE64242);
  static const Color _titleColor = Color(0xFF787878);
  static const double _titleFontSize = 12.0;

  //弹出底部文字
  static void show(
    BuildContext context, {
    String? title,
    List<String>? dataArr,
    String? redBtnTitle,
    bool isShowRadius = true,
    Color? background,
    Color lineColor = const Color(0xffF6f6f6),
    Color textColor = const Color(0xff222222),
    String cancelText = 'Cancel',
    SheetClickCallBack? clickCallback,
  }) {
    List<String> _dataArr = [];

    if (dataArr != null) {
      _dataArr = dataArr;
    }
    if (redBtnTitle != null) {
      _dataArr.insert(_dataArr.length, redBtnTitle);
    }
    var titleHeight = _cellHeight;
    var titleLineHeight = 0.5;
    if (title == null) {
      titleHeight = 0.0;
      titleLineHeight = 0.0;
    }

    var _bgHeight = _cellHeight * (_dataArr.length + 1) +
        (_dataArr.length - 1) * 1 +
        _spaceHeight +
        titleHeight +
        titleLineHeight;

    var _radius = isShowRadius ? 10.0 : 0.0;

    showModalBottomSheet(
        context: context,
        //设置圆角
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(_radius),
            topRight: Radius.circular(_radius),
          ),
        ),
        // 抗锯齿
        clipBehavior: Clip.antiAlias,
        builder: (BuildContext context) {
          return Container(
              color: background ?? Colors.white,
              child: SafeArea(
                  child: SizedBox(
                height: _bgHeight,
                child: Column(
                  children: <Widget>[
                    SizedBox(
                      height: titleHeight,
                      child: Center(
                        child: Text(
                          title ?? "",
                          style: const TextStyle(
                              fontSize: _titleFontSize, color: _titleColor),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ),
                    SizedBox(
                      height: titleLineHeight,
                      child: Container(color: lineColor),
                    ),
                    ListView.separated(
                      itemCount: _dataArr.length,
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemBuilder: (BuildContext context, int index) {
                        Color _btnTextColor = (redBtnTitle != null &&
                                index == _dataArr.length - 1)
                            ? _redTextColor
                            : textColor;

                        return GestureDetector(
                          child: Container(
                              height: _cellHeight,
                              color: background,
                              child: Center(
                                  child: Text(_dataArr[index],
                                      style: TextStyle(
                                          fontSize: _textFontSize,
                                          color: _btnTextColor),
                                      textAlign: TextAlign.center))),
                          // onTap: () => Navigator.of(context).pop(index),
                          onTap: () {
                            Navigator.of(context).pop(index);
                            if (clickCallback != null) {
                              clickCallback(index + 1, _dataArr[index]);
                            }
                          },
                        );
                      },
                      separatorBuilder: (context, index) {
                        return Divider(
                          height: 1,
                          color: lineColor,
                        );
                      },
                    ),
                    SizedBox(
                        height: _spaceHeight,
                        child: Container(color: lineColor)),
                    GestureDetector(
                      child: Container(
                          height: _cellHeight,
                          color: background,
                          child: Center(
                              child: Text(cancelText,
                                  style: TextStyle(
                                      fontSize: _textFontSize,
                                      color: textColor),
                                  textAlign: TextAlign.center))),
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                    )
                  ],
                ),
              )));
        });
  }
}
